﻿using System;
using UnityEngine;

public static class FalloffGenerator {
    /// <summary>
    /// 生成下降贴图
    /// </summary>
    /// <param name="size">边长大小</param>
    /// <returns></returns>
    public static float[, ] GenerateFalloffMap (int size) {
        float[, ] map = new float[size, size];
        float x, y;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // 把0~size映射到-1~1
                x = i / (float) size * 2 - 1;
                y = j / (float) size * 2 - 1;

                // 取最大的绝对值
                map[i, j] = Evaluate (Mathf.Max (Mathf.Abs (x), Mathf.Abs (y)));
            }
        }
        return map;
    }

    /// <summary>
    /// 通过一个数学函数调整下降强度
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private static float Evaluate (float value) {
        float a = 3;
        float b = 2.2f;
        // 函数原理看视频9分钟的地方，
        // https://www.youtube.com/watch?v=COmtTyLCd6I&list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3&index=12&t=31s
        return Mathf.Pow (value, a) / (Mathf.Pow (value, a) + Mathf.Pow (b - b * value, a));
    }
}